package com.chenss.dragonser.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chenss.dragonser.R;
import com.chenss.dragonser.app.AppInfo;
import com.chenss.dragonser.app.Constant;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.model.UserInfoModel;
import com.chenss.dragonser.network.NetworkRequest;
import com.chenss.dragonser.network.RequestCallbcak;
import com.chenss.dragonser.utils.GsonUtil;
import com.chenss.dragonser.utils.ToastUtil;

import org.json.JSONException;

import java.util.logging.Logger;

/**
 * PageName: 修改密码
 * Create by chenss at 2018/4/21
 */
public class UpdatePwdActivity extends BaseActivity implements RequestCallbcak {
    LinearLayout ll_left;//左边按钮
    TextView tv_page_title;
    Button btn_sub;
    EditText edt_old,edt_new,edt_again;

    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装


    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_upd_pwd;
    }

    @Override
    public void initView(View view) {
        ll_left=findView(R.id.ll_left);//左边按钮
        tv_page_title=findView(R.id.tv_page_title);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("修改密码");

        btn_sub=findView(R.id.btn_sub);
        edt_old=findView(R.id.edt_old);
        edt_new=findView(R.id.edt_new);
        edt_again=findView(R.id.edt_again);
        btn_sub.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        networkRequest = new NetworkRequest(mContext, this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()){
            case R.id.ll_left:
                finish();
                break;
         case R.id.btn_sub:
             if (TextUtils.isEmpty(edt_old.getText().toString())) {
                 ToastUtil.showShort(this, "请输入原密码");
                 return;
             }
             if (TextUtils.isEmpty(edt_new.getText().toString())) {
                 ToastUtil.showShort(this, "请输入新密码");
                 return;
             }
             if (TextUtils.isEmpty(edt_again.getText().toString())) {
                 ToastUtil.showShort(this, "请再次输入新密码");
                 return;
             }
             if (!edt_new.getText().toString().equals(edt_again.getText().toString())) {
                 ToastUtil.showShort(this, "两次输入的密码不一致，请重新输入");
                 return;
             }
           doUpdatePwd();
                break;
        }
    }
    /**
     * 修改密码
     */
    private void doUpdatePwd() {
//        store_id  用户id
//        password 旧密码
//        newpasswor 新密码
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Store, Constant.classes.StoreChangePwd);
        parmMap.put("store_id", AppInfo.getUser(this).getStore_id());
        parmMap.put("password", edt_old.getText().toString());
        parmMap.put("newpassword", edt_new.getText().toString());
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, true, Constant.classes.StoreChangePwd);
    }

    @Override
    public void onFaild(int orderCode, String result) {
        ToastUtil.showShort(this, result);
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
                    //修改成功
                 ToastUtil.showShort(UpdatePwdActivity.this,"修改成功！");
                 this.finish();
        }
    }
}
